package com.jy.appmarket.screenlock.activity;

import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.Window;
import android.view.WindowManager;

import com.jy.appmarket.screenlock.LockScreenManager;
import com.jy.appmarket.screenlock.LockScreenManager.ILockScreenActivityListener;
import com.jy.appmarket.screenlock.LockSoundManager;
import com.jy.appmarket.screenlock.MainLockViewManager;
import com.jy.appmarket.screenlock.prefs.LockPrefs;
import com.jy.appmarket.statistics.event.ScreenLockEvent;
import com.jy.appmarket.statistics.event.ScreenUnlockEvent;
import com.jy.appmarket.statistics.umeng.myUmengStaticstics;
import com.jy.appmarket.utils.LogUtil;
import com.rad.statistics.android.StatisticsAgent;
import com.umeng.analytics.MobclickAgent;

/**
 * 类说明： 锁屏界面
 * 
 * @author xinhui.cheng
 * @date 2015-2-9
 * @version 1.0
 */
public class LockscreenShowActivity extends Activity {

	public static final String ACTION_FINISH_SHOW = "com.jy.appmarket.action.FINISH_LOCK_SHOW";

	private static final String TAG = LockscreenShowActivity.class.getSimpleName();
	private ProtectThread mThread;

	private MainLockViewManager mMainLockManager;

	private View mLockView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initLockScreenViews();
		if (!LockScreenManager.getInstance(this).isLocked()) {
			finish();
		}

		if (Build.VERSION.SDK_INT >= 19) {
			Window window = getWindow();
			window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
		}

		setContentView(mLockView);
		registerReceiver(mFinishReceiver, new IntentFilter(ACTION_FINISH_SHOW));
		mThread = new ProtectThread();
		mThread.start();

		LockScreenManager.getInstance(this).setOnLockScreenActivityListener(new ILockScreenActivityListener() {
			@Override
			public void onChangeWallpaper() {
				if (mMainLockManager != null) {
					mMainLockManager.changeWallpaper();
					mMainLockManager.openSliding(false);
				}
			}
		});

		myUmengStaticstics.screenLock(getApplicationContext(), "activity");
		StatisticsAgent.onEvent(getApplicationContext(), new ScreenLockEvent("activity"));
	}

	private void initLockScreenViews() {
		mMainLockManager = new MainLockViewManager(this, false);
		mLockView = mMainLockManager.getMainLockView();
	}

	private class ProtectThread extends Thread {
		@Override
		public void run() {
			while (true) {
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					break;
				}

				ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
				List<RunningTaskInfo> tasks = am.getRunningTasks(10);
				RunningTaskInfo rti = tasks.get(0);
				if (getTaskId() == -1) {
					break;
				}
				if (rti.id != getTaskId()) {
					am.moveTaskToFront(getTaskId(), ActivityManager.MOVE_TASK_NO_USER_ACTION);
				}
			}
			runOnUiThread(new Runnable() {
				public void run() {
					finish();
				}
			});

		}
	}

	protected void onResume() {
		super.onResume();
		int systemUI = View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
		if (!ViewConfiguration.get(this).hasPermanentMenuKey()) {
			systemUI |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
		}
		// systemUI |= View.SYSTEM_UI_FLAG_FULLSCREEN;
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		if (Build.VERSION.SDK_INT >= 19) {
			systemUI |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
		}
		getWindow().getDecorView().setSystemUiVisibility(systemUI);
		MobclickAgent.onResume(this); // 统计时长
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return true;
	}

	@Override
	protected void onDestroy() {
		LogUtil.d(TAG, "onDestroy");
		if (mThread != null) {
			mThread.interrupt();
			mThread = null;
		}

		unregisterReceiver(mFinishReceiver);
		mMainLockManager.unregisterReceiver();
		mMainLockManager.ClearMessageView();
		boolean lockScreenVoice = LockPrefs.isLockSoundOpen(this);
		if (lockScreenVoice) {
			LockSoundManager.play(this, LockSoundManager.SOUND_ID_UNLOCK);
		}
		LockScreenManager.getInstance(getApplicationContext()).unLock(true);

		myUmengStaticstics.screenUnlock(getApplicationContext(), "activity");
		StatisticsAgent.onEvent(getApplicationContext(), new ScreenUnlockEvent("activity"));
		super.onDestroy();
	}

	private BroadcastReceiver mFinishReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (mThread != null) {
				mThread.interrupt();
				mThread = null;
			}
		}
	};

}